/*
 *******************************************************************
 *
 * COPYRIGHT (coffee) 2013 - 2014 LTV Co., Ltd  All rights reserved
 *
 * Created  : Mar 23, 2014
 * Change History
 * =================================================================
 * DATE                DEVELOPER        REVISION      DESCRIPTION
 * =================================================================
 * Mar 23, 2014        ducnguyen        1.0           First Created.
 * =================================================================
 *
 *
 *******************************************************************/
package vn.com.ltv.lo.daos;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import vn.com.ltv.lo.common.FileManager;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

class DatabaseHelper extends SQLiteOpenHelper {
	private String databasePath;
	protected static String DATABASE_NAME = "OP.db";
	protected static int DATABASE_VERSION = 1;
	private Context context;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

		this.context = context;

		this.databasePath = this.context.getDatabasePath(DATABASE_NAME)
				.getPath();
	}

	// Copy database from assets-folder to just created empty database

	private void copyDatabase() throws IOException {
		FileManager.createFolder(new File(this.databasePath).getParent());

		InputStream inputStream = this.context.getAssets().open(DATABASE_NAME);
		OutputStream outputStream = new FileOutputStream(this.databasePath);

		// transfer byte to inputfile to outputfile
		byte[] buffer = new byte[1024];
		int length;
		while ((length = inputStream.read(buffer)) > 0) {
			outputStream.write(buffer, 0, length);
		}

		// Close the streams
		outputStream.flush();
		outputStream.close();
		inputStream.close();
	}

	@Override
	public synchronized SQLiteDatabase getReadableDatabase() {
		// String path = android.os.Environment.getExternalStorageDirectory() +
		// "/" + DATABASE_NAME + ".sqlite";
		File file = new File(this.databasePath);
		if (!file.exists()) {
			try {
				this.copyDatabase();
				// Log.d("Copy database " + DATABASE_NAME, "ok");
			} catch (IOException e) {
				e.printStackTrace();
				// Log.d("Copy database " + DATABASE_NAME, "fail");
			}
		}

		SQLiteDatabase sQLiteDatabase = SQLiteDatabase.openDatabase(
				databasePath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS
						| SQLiteDatabase.CREATE_IF_NECESSARY
						| SQLiteDatabase.OPEN_READONLY);

		return sQLiteDatabase;
	}

	@Override
	public synchronized SQLiteDatabase getWritableDatabase() {
		// String path = android.os.Environment.getExternalStorageDirectory() +
		// "/" + DATABASE_NAME + ".sqlite";
		File file = new File(this.databasePath);
		if (!file.exists()) {
			try {
				this.copyDatabase();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		SQLiteDatabase sQLiteDatabase = SQLiteDatabase.openDatabase(
				databasePath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS
						| SQLiteDatabase.CREATE_IF_NECESSARY
						| SQLiteDatabase.OPEN_READWRITE);

		return sQLiteDatabase;
	}

	@Override
	public void onCreate(SQLiteDatabase sQLiteDatabase) {
//		sQLiteDatabase.execSQL(BillDAO.TABLE_CREATION_STRING);
//		sQLiteDatabase.execSQL(BillDetailDAO.TABLE_CREATION_STRING);
//		sQLiteDatabase.execSQL(CommentDAO.TABLE_CREATION_STRING);
//		sQLiteDatabase.execSQL(DishDAO.TABLE_CREATION_STRING);
//		sQLiteDatabase.execSQL(ImageDAO.TABLE_CREATION_STRING);
//		sQLiteDatabase.execSQL(ImageDiskDAO.TABLE_CREATION_STRING);
//		sQLiteDatabase.execSQL(LevelDAO.TABLE_CREATION_STRING);
//		sQLiteDatabase.execSQL(RatingDAO.TABLE_CREATION_STRING);
//		sQLiteDatabase.execSQL(SetDAO.TABLE_CREATION_STRING);
//		sQLiteDatabase.execSQL(SetListDAO.TABLE_CREATION_STRING);
//		sQLiteDatabase.execSQL(UserDAO.TABLE_CREATION_STRING);
//		sQLiteDatabase.execSQL(CategoryDAO.TABLE_CREATION_STRING);
	}

	@Override
	public void onUpgrade(SQLiteDatabase sQLiteDatabase, int oldVersion,
			int newVersion) {
		//sQLiteDatabase.execSQL(EnglishcommonDAO.TABLE_DROP_STRING);
	}
}